class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB)
    {
        int alen = 0, blen = 0;
        ListNode *pa = headA, *pb = headB;
        while(headA) {
            alen++;
            headA = headA->next;
        }
        while(headB) {
            blen++;
            headB = headB->next;
        }

        headA = pa;
        headB = pb;
        if(alen > blen) {
            int t = alen - blen;
            while(t--) {
                headA = headA->next;
            }
        } else {
            int t = blen - alen;
            while(t--) {
                headB = headB->next;
            }
        }
        while(headA && headB) {
            if(headA == headB) {
                return headA;
            }
            headA = headA->next;
            headB = headB->next;
        }
        return NULL;
    }
};